package cst.a722wr.zyfypt_722wr.Fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
 
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a722wr.zyfypt_722wr.Adapter.ArticleAdapter;
import cst.a722wr.zyfypt_722wr.Bean.ArticleBean;
import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.wrListener;
import cst.a722wr.zyfypt_722wr.model.ResModel;


public class ArticleFragment extends Fragment {

    private RecyclerView recyclerView;
    private ArticleAdapter adapter;
    private List<ArticleBean> list;
    private LinearLayoutManager layoutManager;
    private wrListener<List<ArticleBean>> listener;
    private ResModel model;
    private SharedPreferences sp;
    private int page = 1;
    private int lastVisibleItemPosition;
    private String sessionID;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.article_rec);

        sp = getActivity().getSharedPreferences("ME", Context.MODE_PRIVATE);
        sessionID = sp.getString("session_id", null);

        adapter = new ArticleAdapter(getActivity());
        list = new ArrayList<ArticleBean>();
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        listener = new wrListener<List<ArticleBean>>() {
            @Override
            public void onResponse(List<ArticleBean> l) {
                if(page == 1) {
                    list = l;
                } else {
                    list.removeAll(l);
                    list.addAll(l);
                }
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        };

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    model.getList("article", page, listener);
                    System.out.println("----onScrollStateChanged  page="+page);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();
            }
        });
        model = new ResModel("gson", sessionID);
        model.getList("article", page, listener);
        return view;
    }

}
